﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Daneas.Utility.Universals
{
    #region Enum

    [AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field, AllowMultiple = false)]
    public class EnumDesAttribute : Attribute
    {
        private string description;

        public string Description
        {
            get { return this.description; }

        }

        public EnumDesAttribute(string description)
            : base()
        {
            this.description = description;
        }
    }

    public class EnumDes : Attribute
    {
        private string m_strDescription;

        public EnumDes(string strEnumDescription)
        {
            m_strDescription = strEnumDescription;
        }

        public string Description
        {
            get { return m_strDescription; }
        }

        /// <summary>
        /// 获取枚举描述（附例）
        /// string desc = EnumDescriptionAttribute.GetEnumDescription(EmployeeType.DepartmentManager);
        /// </summary>
        /// <param name="enumObj"></param>
        /// <returns></returns>
        public static string GetEnumDescription(Enum enumObj)
        {
            FieldInfo fieldInfo = enumObj.GetType().GetField(enumObj.ToString());

            object[] attribArray = fieldInfo.GetCustomAttributes(false);

            if (attribArray.Length == 0)
            {
                return String.Empty;
            }
            else
            {
                EnumDesAttribute attrib = attribArray[0] as EnumDesAttribute;

                return attrib.Description;
            }
        }

        /// <summary>
        /// 获取对象特性(附例)
        ///ManagerAttribute manager = EnumDescriptionAttribute.GetAttribute<ManagerAttribute>(EmployeeType.DepartmentManager);
        ///Console.WriteLine("Manager: {0}, Min Salary: {1}, Max Salary {2}",
        ///attrib.Description,
        ///manager.MinSalary,
        ///manager.MaxSalary);
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumObj"></param>
        /// <returns></returns>
        public static T GetAttribute<T>(Enum enumObj) where T : EnumDes
        {
            FieldInfo fieldInfo = enumObj.GetType().GetField(enumObj.ToString());

            object[] attribArray = fieldInfo.GetCustomAttributes(false);

            if (attribArray.Length == 0)
            {
                return default(T);
            }
            else
            {
                T attrib = (T)attribArray[0];

                if (attrib != null)
                {
                    return attrib;
                }
                else
                {
                    return default(T);
                }
            }
        }

        /// <summary>
        /// 获取枚举列表(有问题的)
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns>枚举列表</returns>
        public static Dictionary<string, string> GetEnumDictionary(Type enumType)
        {
            Dictionary<string, string> enumDic = new Dictionary<string, string>();

            FieldInfo[] fileInfos = enumType.GetFields();

            foreach (FieldInfo fileInfo in fileInfos)
            {
                object[] attrs = fileInfo.GetCustomAttributes(typeof(EnumDes), false);

                if (attrs == null || attrs.Length == 0)
                {
                    continue;
                }

                string display = ((EnumDes)(attrs[0])).Description;

                string value = fileInfo.Name.ToString();

                enumDic.Add(display, value);
            }
            return enumDic;
        }

        /// <summary>
        /// 获取枚举列表
        /// </summary>
        /// <param name="enumType">枚举类型</param>
        /// <returns></returns>
        public static Dictionary<string, int> GetItems(Type enumType)
        {
            if (!enumType.IsEnum)
                throw new InvalidOperationException();

            Dictionary<string, int> dic = new Dictionary<string, int>();

            // 获取Description特性 
            Type typeDescription = typeof(EnumDesAttribute);
            // 获取枚举字段
            FieldInfo[] fields = enumType.GetFields();
            foreach (FieldInfo field in fields)
            {
                if (!field.FieldType.IsEnum)
                    continue;

                // 获取枚举值
                int value = (int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null);

                // 不包括空项
                if (value >Int32.MinValue&& value<Int32.MaxValue)
                {
                    string text = string.Empty;
                    object[] array = field.GetCustomAttributes(typeDescription, false);

                    if (array.Length > 0) text = ((EnumDesAttribute)array[0]).Description;
                    else text = field.Name; //没有描述，直接取值


                    //添加到列表
                    dic.Add(text, value);
                }
            }
            return dic;
        }

    }
    #endregion
}
